package com.es.elasticsearchdemo;

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author : yx-0176
 * @description
 * @date : 2021/11/1
 */
@Service
public class EsServiceImpl implements EsService {
    private RestHighLevelClient restHighLevelClient;

    public EsServiceImpl(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public void createIndex(String indexName) throws IOException {
        // 1. 创建索引请求
        CreateIndexRequest firstIndex = new CreateIndexRequest(indexName);

        // 2. 客户端执行创建索引的请求
        CreateIndexResponse response = restHighLevelClient.indices().create(firstIndex, RequestOptions.DEFAULT);

        System.out.println(response);
    }

    @Override
    public void delIndex(String indexName) throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(delete);
    }



    @Override
    public boolean existsIndex(String indexName) throws IOException {
        // 1. 创建一个get请求获取指定索引的信息
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);

        // 2. 客户端执行请求判断索引是否存在
       return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    }

    @Override
    public void addDocument(String indexName,String docName, Object obj) throws IOException {

        // 2. 创建请求并指定索引
        IndexRequest indexRequest = new IndexRequest(indexName);

        // 3. 创建的规则：put /indexName/_doc/docName
        // 设置ID
        indexRequest.id(docName);
        // 设置超时时间
        indexRequest.timeout(TimeValue.timeValueSeconds(1));

        // 4. 将数据放入到请求中
        indexRequest.source(JSON.toJSONString(obj), XContentType.JSON);

        // 5. 使用客户端发送请求
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(index));
    }


    @Override
    public void updateDocment(String indexName,String docName, Object obj) throws IOException {
        // 1. 创建一个更新请求的信息，绑定索引名称和需要更新的文档ID
        UpdateRequest updateRequest = new UpdateRequest(indexName, docName);
        // 设置超时时间
        updateRequest.timeout(TimeValue.timeValueSeconds(1));
        // 2. 封装需要更新的文档信息
        updateRequest.doc(JSON.toJSONString(obj), XContentType.JSON);

        // 3. 使用update更新文档
        UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(updateResponse));
    }

    @Override
    public void deleteDocment(String indexName, String docName) throws IOException {
        // 1. 创建一个删除的请求，绑定索引名和需要删除的文档ID
        DeleteRequest deleteRequest = new DeleteRequest(indexName, docName);


        // 2. 发起删除请求
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(response));
    }

    @Override
    public void getDocument(String indexName, String docName) throws IOException {
        GetRequest getRequest = new GetRequest(indexName,docName);
        GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields.getSourceAsString());
        System.out.println(documentFields);
    }

    @Override
    public void search(String indexName,TermQueryBuilder query) throws IOException {
        // 1. 创建批量搜索请求，并绑定索引
        SearchRequest searchRequest = new SearchRequest(indexName);

        // 2. 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        sourceBuilder.query(query).timeout(TimeValue.timeValueSeconds(60));

        // 3. 将查询条件放入搜索请求request中
        searchRequest.source(sourceBuilder);

        // 4. 发起查询请求获取数据
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(response));
        System.out.println(JSON.toJSONString(response.getHits().getHits()));

    }

    @Override
    public void searchHighLight(String indexName, TermQueryBuilder query) throws IOException {
        // 1. 创建批量搜索请求，并绑定索引
        SearchRequest searchRequest = new SearchRequest(indexName);

        // 2. 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 文字高亮
        sourceBuilder.highlighter();

        sourceBuilder.query(query).timeout(TimeValue.timeValueSeconds(60));

        // 3. 将查询条件放入搜索请求request中
        searchRequest.source(sourceBuilder);

        // 4. 发起查询请求获取数据
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    }
}
